var layerId;
var clickState =true;
$(function(){
	$("#add-goods-button").click(function(){
		var chooseType = "list";
		layerId=layer.open({
			area: ['1000px','600px'],
			fix:true,
			type: 2,
			content: basePath + '/goods/queryGoodsList.shtml?chooseType='+chooseType
		})
	});
	$("#add-group-button").click(function(){
		var chooseType = "list";
		layerId=layer.open({
			area: ['1000px','600px'],
			fix:true,
			type: 2,
			content: basePath + '/group/queryGroupList.shtml?chooseType='+chooseType
		})
	});
	$("#save-button").click(function(){
		var content = $("#content").val();
		var title = $("#title").val();
		var goodsTitle = $("#goodsTitle").val();
		var image = $("#coverImage").attr("data-img");
		var tag = $("#tag").val();
		if(title.length == 0){
			layer.msg("请输入标题");
			return false;
		}
		if(goodsTitle.length == 0){
			layer.msg("请输入推荐商品标题");
			return false;
		}
		if(image.length == 0){
			layer.msg("请输入上传封面图");
			return false;
		}
		var delId = init.delId;
		if(delId.length>0){
			delId=delId.substring(1,delId.length);
		}
		var topicGoodsArray = new Array();
		var topicType = init.topicType;
		var topicId = init.topicId;
		if(topicType=="goods"){
			var data=$(".goods-div table tbody").find("tr");
			if(data.length>0){
				for(var i=0;i<data.length;i++){
					var id=$(data[i]).attr("data-id");
					var dataId=$(data[i]).attr("data-dataId");
					var dataType=1;
					var goodsName=$(data[i]).attr("data-goodsName");
					var price=$(data[i]).attr("data-price");
					var coverImage=$(data[i]).attr("data-image");
					var goods = {
							"id":id,
							"topicId":topicId,
							"dataId":dataId,
							"dataType":dataType,
							"goodsName":goodsName,
							"goodsPrice":price,
							"coverImage":coverImage
					}
					topicGoodsArray.push(goods);
				}
			}
		}else{
			var data=$(".group-div table tbody").find("tr");
			if(data.length>0){
				for(var i=0;i<data.length;i++){
					var id=$(data[i]).attr("data-id");
					var dataId=$(data[i]).attr("data-dataId");
					var dataType=2;
					var style=$(data[i]).attr("data-groupStyle");
					var scene=$(data[i]).attr("data-groupScene");
					var customer=$(data[i]).attr("data-groupCustomer");
					var coverImage=$(data[i]).attr("data-image");
					var group = {
							"id":id,
							"topicId":topicId,
							"dataId":dataId,
							"dataType":dataType,
							"groupStyle":style,
							"groupScene":scene,
							"groupCustomer":customer,
							"coverImage":coverImage
					}
					topicGoodsArray.push(group);
				}
			}
		}
		var topicJson ={
			"topicId":topicId,
			"title":title,
			"goodsTitle":goodsTitle,
			"coverImage":image,
			"tag":tag,
			"content":content,
			"topicType":topicType,
			"topicGoodsList":topicGoodsArray,
			"delIds":delId
		}
		if(clickState){
			clickState =false;
			$.ajax({
				url:basePath+"/topic/save.shtml",
				data:{"topicJson": JSON.stringify(topicJson)},
				type:"post",
				dataType:"JSON",
				success:ajaxSuccessCloseFun
			});
		}
	});
	$("#close-button").click(function(){
		parent.closeLayer(0);
	});
	
});

function deleteData(e){
	var id = $(e).parent().parent().attr("data-id");
	if(id.length>0){
		init.delId =init.delId+","+id
	}
	$(e).parent().parent().remove();
}
function closeGoodsLayer(flag,goodsArray){
	if(flag==1){
		var html="";
		for(i=0;i<goodsArray.length;i++){
			var goodsId = goodsArray[i].id;
			var goodsName = goodsArray[i].name;
			var coverImage = goodsArray[i].coverImage;
			var price = goodsArray[i].price;
			var imageUrl=basePath+"/images/default_goods.jpg";
			if(coverImage != null){
				imageUrl = imgUrl + coverImage;
			}
			html+="<tr data-dataId='"+goodsId+"' data-type='1' data-goodsName='"+goodsName+"' data-price='"+price+"' data-image='"+coverImage+"'>"+
		  		'<td><img  src="'+imageUrl+'" height="50px" width="50px"  data-toggle="modal" data-target="#imgModal"/> </td>'+
				'<td>'+goodsName+'</td>'+
		  		'<td>'+price+'</td>'+
		  		'<td><button type="button" class="layui-btn layui-btn-normal" onclick="deleteData(this)">删除</button></td>'
		  		+'</tr>';
		}
		$(".goods-div table tbody").append(html);
	}
	layer.close(layerId);
}
function closeGroupLayer(flag,groupArray){
	if(flag==1){
		var html="";
		for(i=0;i<groupArray.length;i++){
			var groupId = groupArray[i].id;
			var style = groupArray[i].style;
			var scene = groupArray[i].scene;
			var customer = groupArray[i].customer;
			var coverImage = groupArray[i].coverImage;
			var imageUrl=basePath+"/images/default_goods.jpg";
			if(coverImage != null){
				imageUrl = imgUrl + coverImage;
			}
			html+='<tr data-dataId="'+groupId+'" data-type="2" data-groupStyle="'+style+'" data-groupScene="'+scene+'" data-groupCustomer="'+customer+'"'+
 			  	 'data-image="'+coverImage+'">'+
 			  		'<td><img  src="'+imageUrl+'" height="50px" width="50px"  data-toggle="modal" data-target="#imgModal"/></td> '+
					'<td>'+style+'</td>'+
					'<td>'+scene+'</td>'+
					'<td>'+customer+'</td>'+
 			  		'<td><button type="button" class="layui-btn layui-btn-normal" onclick="deleteData(this)">删除</button></td>'+
 			  	'</tr>';
		}
		$(".group-div table tbody").append(html);
	}
	layer.close(layerId);
}